﻿using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        /// <summary>
        /// Adjusts main form'pShift UI accordingly to privileges of currently logged user
        /// </summary>
        /// <remarks>
        /// 1.  This method disables buttons, 
        /// 2.  There are two different privilege levels When it comes to editing rotas
        ///     and editing holidays - our user may be able only to edit current / future rotas
        ///     (but not rotas for the past weeks - not to modify hours that have already been worked),
        ///     the same with holidays: you may be allowed to book some more holidays for any _employee,
        ///     but not to, for example, delete any holidays that the _employee has already presumably
        ///     returned from.
        ///     Those privileges are UserPrivilege.EditingRotas / EditingPastRotas
        ///     and UserPrivilege.EditingHolidays / EditingPastHolidays.
        ///     
        ///     Since our user may be browsing rotas one by one, we cannot just fix the datagridview ReadOnly
        ///     (or not) once and forever. Therefore, the LoadRota method checks if the loaded rota is for one of
        ///     the past weeks, or current/future one, and reacts to that.
        ///     
        ///     In case of Holidays: ImprovedListView control is equipped with a delegate Editable
        /// 3.  Called subroutines also should have check for it, to make sure
        ///     
        /// </remarks>
        public void DisableUnaccessibleUIControls()
        {
            #region menu
            var _can_edit_something = Program.Logged.CanEditTemplates;
            miFileNew.Enabled = Program.Logged.CanSaveDatafiles;
            miFileSave.Enabled = Program.Logged.CanSaveDatafiles;
            miFileSaveAs.Enabled = Program.Logged.CanSaveDatafiles;
            miFileImportTemplates.Enabled = Program.Logged.CanSaveDatafiles;
            #endregion
            #region staff
            if (!Program.Logged.CanViewStaff)
            {
                CollapseStaffDetails();
            }
            else
            {
                UncollapseStaffDetails();
            }

            
            btnAddEmployee.Enabled = Program.Logged.CanEditStaff;
            btnEditEmployee.Enabled = Program.Logged.CanEditStaff;
            btnChangeContract.Enabled = Program.Logged.CanEditContracts;
            #endregion

            #region rota
            /* NOTE: this is done in LoadRota as well, and so it should be,
             * as our user may be allowed to edit current/future rotas - but
             * not the ones from the past. So it needs to be checked every Time
             * for every loaded rota individually.
             * 
             * However, if user logs to another account (via menu bar) while editing
             * CurrentRota, LoadRota does not actually get called.*/
            svRota.ReadOnly = !Program.Logged.CanEditRota(CurrentRota.Monday);
            btnInventories.Enabled = !svRota.ReadOnly;
            btnShutDays.Enabled = !svRota.ReadOnly;
            btnGenerate.Enabled = !svRota.ReadOnly;
            btnNotes.Enabled = !svRota.ReadOnly;
            
            #endregion
            #region templates
            svTemplate.ReadOnly = !Program.Logged.CanEditTemplates;
            btnNewTemplate.Enabled = Program.Logged.CanEditTemplates;
            btnDeleteTemplate.Enabled = Program.Logged.CanEditTemplates;
            btnSaveTemplate.Enabled = Program.Logged.CanEditTemplates;
            #endregion
                       

            #region editing holidays
            
            
            /* in principle, our user shouldn't be entitled to editing past holidays
             * whilst disallowed to edit holidays in general - but we'll still take such
             * bizarre configuration into account */            
            var _can_edit_any_holidays = (Program.Logged.CanEditHolidays |
                Program.Logged.CanEditPastHolidays);
            /* user'pShift entitlement to edit or delete holidays may vary,
             * if he'pShift only allowed to edit past ones.
             * adding holidays, however, is a different story, because When we're about
             * to AddAnotherRowString, we don't know in advance yet what Time period it will be in
             * (could be future) */
            
            btnManageHolidays.Enabled = _can_edit_any_holidays;
            #endregion

            #region requests
            tbMain.TabPages[4].Enabled = Program.Logged.CanViewRequests;
            commentsDataGridViewTextBoxColumn.Visible = Program.Logged.CanEditRequests;
            colRequestsPendingComments.Visible = Program.Logged.CanEditRequests;
            dgridRequests_SelectionChanged(null, null);
            dgridRequestsPending_SelectionChanged(null, null);
            #endregion

            #region contracts
            btnChangeContract.Enabled = Program.Logged.CanEditContracts;
            #endregion

            if (!tbMain.TabPages[tbMain.SelectedIndex].Enabled)
            {
                SwitchToTab(tpCalendar);
            }
        }

        /// <summary>
        /// Allows the user to log in 
        /// </summary>
        private void Relog()
        {
            if (CurrentRota.OKLeaving()
                && CurrentTemplate.OKLeaving())
            {
                Program.frmLogon = new Logon();
                Hide();
                if (Program.frmLogon.ShowDialog() == DialogResult.OK)
                {
                    DisableUnaccessibleUIControls();
                    CurrentFile_OnRotasCollectionModified();

                    sheetTempOpeningHrs.Bind(
                        CurrentTemplate,
                        svTemplate);
                    UpdateTitlebar();
                }
                Show();
            }            
        }
    }
}
